﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;
using DBA.Web.UI;
using DBA.Bll;

namespace DBA.Web.SystemManage.EmployeeManage
{
    public partial class EmployeeManage : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["showType"] = UtilsHelper.Decode(Request.Params["type"]);
            if (IsPostBack) return;
            ddlBind();
            IDictionary<String, object> dict = new Dictionary<string, object>();
            switch (ViewState["showType"].ToString())
            {
                case "0": //查看所有教师信息                    
                    break;
                case "1": //查看当前系的所有教师信息
                    ddlXi.SelectedValue = ServerContext.AdminCurrentUser.Xi_ID.ToString();
                    li_add.Visible = false;
                    li_del.Visible = false;
                    ddlXi.Enabled = false;
                    break;
                case "2": //查看当前教师自己的信息
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    ddlXi.SelectedValue = ServerContext.AdminCurrentUser.Xi_ID.ToString();
                    li_add.Visible = false;
                    li_del.Visible = false;
                    ddlXi.Enabled = false;
                    break;
            }
            Bind(1, dict);
        }

        protected void linkBtnSearch_Click(object sender, EventArgs e)
        {
            IDictionary<String, object> dict = new Dictionary<String, object>();
            switch (ViewState["showType"].ToString())
            {              
                case "2": //查看当前教师自己的信息
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    break;
            }
            dict.Add("Name", txtName.Text.Trim());
            dict.Add("Sex", ddlSex.SelectedValue);
            dict.Add("Xi_ID", ddlXi.SelectedValue);
            dict.Add("Suo_ID", ddlSuo.SelectedValue);
            dict.Add("Bachelor", txtBal.Text.Trim());
            dict.Add("jszwzgID", ddlJszwzg.SelectedValue);
            dict.Add("time1", txtBirthDay1.Value);
            dict.Add("time2", txtBirthDay2.Value);
            ViewState["conditions"] = dict;
            Bind(1, dict);
        }

        protected void ddlBind() 
        {
            BaseCodeDDL.DdlInit(ddlXi, "Xi");//绑定系
            BaseCodeDDL.DdlInit(ddlSuo, "Suo");         
            BaseCodeDDL.DdlInit(ddlJszwzg, "JSZWZG");
           
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            IDictionary<String, object> dict = null;
            if (ViewState["conditions"] != null)
            {
                dict = (IDictionary<String, object>)ViewState["conditions"];
            }
            else
            {
                dict = new Dictionary<string, object>();
                switch (ViewState["showType"].ToString())
                {
                    case "1": //查看当前教师自己的信息
                        dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                        break;       
                }
            }
            Bind(e.NewPageIndex, dict);
        }

        private void Bind(int pageIdx, IDictionary<String, object> dict)
        {

            string sql = "select a.*,b.Name as XiName,c.Name as SuoName,d.Name as DptName,f.name as PartName,g.Name as JsName from  ";
            sql += "EmployeeInfo a left join Xi b on a.Xi_ID=b.Xi_ID  left join Suo c on a.Suo_ID=c.Suo_ID ";
            sql += "left join sys_department d on a.dpt_id=d.dpt_id  left join PartyDuty f on a.PD_id=f.PD_id ";
            sql += "left join JSZWZG g on a.jszwzgID=g.jszwzgID left join EduAttainments h on a.EaID=h.EaID ";
            sql += " where 1=1 ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            switch (ViewState["showType"].ToString())
            {              
                case "2"://查看当前教师自己的服务工作量
                    if (!VerificationHelper.IsNullOrEmpty(dict["EmployeeID"]))
                    {
                        sql += " and a.EmployeeID = @EmployeeID";
                        dal.ManagerParameter.AddNewInParameter("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    }
                    break;
            }
            if (dict != null)
            {
                if (dict.ContainsKey("Name"))
                {
                    sql += " and a.Name like '%'+@Name+'%'";
                    dal.ManagerParameter.AddNewInParameter("Name", dict["Name"].ToString());
                }
                if (dict.ContainsKey("Sex") && !dict["Sex"].ToString().Equals("-1"))
                {
                    sql += " and a.Sex =@Sex";
                    dal.ManagerParameter.AddNewInParameter("Sex", int.Parse(dict["Sex"].ToString()));
                }

                if (dict.ContainsKey("Xi_ID") && !dict["Xi_ID"].ToString().Equals("-1"))
                {
                    sql += " and a.Xi_ID =@Xi_ID";
                    dal.ManagerParameter.AddNewInParameter("Xi_ID", dict["Xi_ID"].ToString());
                }

                if (dict.ContainsKey("Suo_ID") && !dict["Suo_ID"].ToString().Equals("-1"))
                {
                    sql += " and a.Suo_ID =@Suo_ID";
                    dal.ManagerParameter.AddNewInParameter("Suo_ID", dict["Suo_ID"].ToString());
                }

                if (dict.ContainsKey("Bachelor") && !dict["Bachelor"].ToString().Equals(string.Empty))
                {
                    sql += " and a.Bachelor =@Bachelor";
                    dal.ManagerParameter.AddNewInParameter("Bachelor", dict["Bachelor"].ToString());
                }

                if (dict.ContainsKey("jszwzgID") && !dict["jszwzgID"].ToString().Equals("-1"))
                {
                    sql += " and a.jszwzgID =@jszwzgID";
                    dal.ManagerParameter.AddNewInParameter("jszwzgID", dict["jszwzgID"].ToString());
                }

                if (dict.ContainsKey("time1") && !dict["time1"].ToString().Equals(string.Empty))
                {
                    sql += " and a.BirthDay >=@BirthDay1";
                    dal.ManagerParameter.AddNewInParameter("BirthDay1", dict["time1"].ToString());
                }

                if (dict.ContainsKey("time2") && !dict["time2"].ToString().Equals(string.Empty))
                {
                    sql += " and a.BirthDay <=@BirthDay2";
                    dal.ManagerParameter.AddNewInParameter("BirthDay2", dict["time2"].ToString());
                }
            }


            int iCount = 0;

            replData.DataSource = dal.ModifyData.GetPageDataTable("(" + sql + ")", "*", "", "EmployeeID DESC", pageIdx, 10, ManagerSQL.PageViewTableType.MixtureTable, ref iCount);
            replData.DataBind();
            AspNetPager1.RecordCount = iCount;

        }

        protected void linkBtnMaskSearch_Click(object sender, EventArgs e)
        {
            //IDictionary<String, String> dict = new Dictionary<String, String>();
            //dict.Add("UserName", txtMaskMemberId.Text.Trim());
            //ViewState["conditions"] = dict;
            //Bind(1, dict);
        }

        protected void linkBtnDeleSelect_Click(object sender, EventArgs e)
        {
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            if (!String.IsNullOrEmpty(hidDeleString.Value))
            {
                string value = hidDeleString.Value;
                string sql = "delete from [EmployeeInfo] where EmployeeID in (" + value + ");";
                dal.ExecuteNonQuery(sql);
                if (String.IsNullOrEmpty(dal.ErrorMessage))
                {
                    Bind(1, null);
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.correct('数据删除成功');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.error('数据删除失败');", true);
                }
            }
        }
        protected void lnkDownload_Click(object sender, EventArgs e)
        {
            IDictionary<String, String> dict = new Dictionary<String, String>();
            dict.Add("Name", txtName.Text.Trim());
            dict.Add("Sex", ddlSex.SelectedValue);
            dict.Add("Xi_ID", ddlXi.SelectedValue);
            dict.Add("Suo_ID", ddlSuo.SelectedValue);
            dict.Add("Bachelor", txtBal.Text.Trim());
            dict.Add("jszwzgID", ddlJszwzg.SelectedValue);
            dict.Add("time1", txtBirthDay1.Value);
            dict.Add("time2", txtBirthDay2.Value);

            string sql = "select a.Name as 姓名,b.Name as 系,c.Name as 所,d.Name as 部门,f.name as 党政职务,g.Name as 技术职务资格,SalaryID 工资号,case sex  when 1 then '男' else '女' end as '性别',Convert(char(10),BirthDay,120) 出生日期,Convert(char(10),jszwzgTime,120)  技术职务资格时间,jszwzgClass 技术职务资格级别,case Degree when 1 then '本科' when 2 then '硕士研究生' when 3 then '博士研究生' end as 最后学历,h.name 最后学位,Convert(char(10),SchoolTime,120) 进校时间,Convert(char(10),JoinWorkTime,120) 参加工作时间,Convert(char(10),JoinPartTime,120) 参加党团时间,Political 政治面貌,TeacherClass 导师类型,Speciality 所学专业,Convert(char(10),GraduteTime,120) 毕业时间,GtaduteSchool 毕业学校,Bachelor 学位名称,Convert(char(10),BachelorTime,120) 授位时间,Reserch 研究方向,Country 国籍,Hometown 籍贯,IDNumber 身份证号,PhoneNum 电话号码,Eamil 邮箱,SubjectPartTime 学术兼职 from  ";
            sql += "EmployeeInfo a left join Xi b on a.Xi_ID=b.Xi_ID  left join Suo c on a.Suo_ID=c.Suo_ID ";
            sql += "left join sys_department d on a.dpt_id=d.dpt_id  left join PartyDuty f on a.PD_id=f.PD_id ";
            sql += "left join JSZWZG g on a.jszwzgID=g.jszwzgID left join EduAttainments h on a.EaID=h.EaID ";
            sql += " where 1=1 ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            if (dict != null)
            {
                if (dict["Name"] != null)
                {
                    sql += " and a.Name like '%'+@Name+'%'";
                    dal.ManagerParameter.AddNewInParameter("Name", dict["Name"].ToString());
                }
                if (!dict["Sex"].ToString().Equals("-1"))
                {
                    sql += " and a.Sex =@Sex";
                    dal.ManagerParameter.AddNewInParameter("Sex", int.Parse(dict["Sex"].ToString()));
                }

                if (!dict["Xi_ID"].ToString().Equals("-1"))
                {
                    sql += " and a.Xi_ID =@Xi_ID";
                    dal.ManagerParameter.AddNewInParameter("Xi_ID", dict["Xi_ID"].ToString());
                }

                if (!dict["Suo_ID"].ToString().Equals("-1"))
                {
                    sql += " and a.Suo_ID =@Suo_ID";
                    dal.ManagerParameter.AddNewInParameter("Suo_ID", dict["Suo_ID"].ToString());
                }

                if (!dict["Bachelor"].ToString().Equals(string.Empty))
                {
                    sql += " and a.Bachelor =@Bachelor";
                    dal.ManagerParameter.AddNewInParameter("Bachelor", dict["Bachelor"].ToString());
                }

                if (!dict["jszwzgID"].ToString().Equals("-1"))
                {
                    sql += " and a.jszwzgID =@jszwzgID";
                    dal.ManagerParameter.AddNewInParameter("jszwzgID", dict["jszwzgID"].ToString());
                }

                if (!dict["time1"].ToString().Equals(string.Empty))
                {
                    sql += " and a.BirthDay >=@BirthDay1";
                    dal.ManagerParameter.AddNewInParameter("BirthDay1", dict["time1"].ToString());
                }

                if (!dict["time2"].ToString().Equals(string.Empty))
                {
                    sql += " and a.BirthDay <=@BirthDay2";
                    dal.ManagerParameter.AddNewInParameter("BirthDay2", dict["time2"].ToString());
                }
            }

            DataTable dt = dal.ExecuteFillDataTable(sql);
            NPOIHelper.ExportByWeb(dt, string.Empty, "教师信息" + new Random().Next().ToString() + ".xls");
        }

        protected void replData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lb_Oper = (Label)e.Item.FindControl("lb_oper");
                switch (ViewState["showType"].ToString())
                {
                    
                    case "0":
                        lb_Oper.Text = "<a href=\"javascript:void(0);\" onclick=\"javascript:turnModifyPage($(this).parent().getTabRowId(),'修改教师信息','EmployeeAdd.aspx');return false;\">修改</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick=\"manageStudy($(this).parent().getTabRowId(),'教师进修情况管理','EmployeeStudyList.aspx');return false;\">管理进修情况</a>"
                + "&nbsp;&nbsp;<a href='javascript:void(0);' onclick=\"manageWork($(this).parent().getTabRowId(),'教师年度工作量设置','EmployeeConfigList.aspx');return false;\">管理工作量设置</a>";
                        break;
                    case "1":
                    case "2":
                        lb_Oper.Text = "<a href=\"javascript:void(0);\" onclick=\"javascript:turnModifyPage($(this).parent().getTabRowId(),'查看教师信息','EmployeeShow.aspx');return false;\">查看</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick=\"manageStudy($(this).parent().getTabRowId(),'教师进修情况查看','EmployeeStudyShow.aspx');return false;\">查看进修情况</a>"
                + "&nbsp;&nbsp;<a href='javascript:void(0);' onclick=\"manageWork($(this).parent().getTabRowId(),'教师年度工作量查看','EmployeeConfigShow.aspx');return false;\">查看工作量设置</a>";
                        break;
                }
            }
        }

    }
}